package org.jackysoft.entity;

import java.util.Collection;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Transient;

import org.apache.struts2.json.annotations.JSON;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.GenericGenerator;
import org.jackysoft.annotations.BID;
import org.jackysoft.util.NoSerialize;
import org.springframework.security.core.GrantedAuthority;

import com.google.common.collect.Lists;


@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
public class AcegiRole  extends AbstractNoIdEntity implements GrantedAuthority {

	/**
	 * 
	 */
	private static final long serialVersionUID = 6202744855301590933L;
	private String name;
	private String description;
    private AcegiRole parent;    
    private Collection<TreeNode> treeNode = Lists.newArrayList();
    private LoginPage login;
    private Integer priority = 0;
    private boolean enabled = true;
    
	public AcegiRole() {}

	public AcegiRole(String name, String description) {
		this.name = name;
		this.description = description;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	@Id@BID
	@GeneratedValue(generator = "agn")
	@GenericGenerator(name = "agn", strategy = "assigned")
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public boolean equals(Object obj) {
		if (obj == null || !(obj instanceof AcegiRole))
			return false;
		AcegiRole o = (AcegiRole) obj;
		if (o.getName() == null && this.getName() == null)
			return false;
		return (o.getName().equals(this.getName()));

	}
	
	public AcegiRole getParent() {
		return parent;
	}

	public void setParent(AcegiRole parent) {
		this.parent = parent;
	}

	public void setTreeNode(Collection<TreeNode> treeNode) {
		this.treeNode = treeNode;
	}

	@ManyToMany(
	       targetEntity = TreeNode.class,
	       fetch=FetchType.LAZY
	)
	@JoinTable(
            name="treenode_acegirole",
            joinColumns = @JoinColumn( name="permissions_name"),
            inverseJoinColumns = @JoinColumn( name="treenode_id")
    )    	
    @NoSerialize
    @JSON(serialize=false)
	public Collection<TreeNode> getTreeNode() {
		return treeNode;
	}

	@Override
	@Transient
	public String getAuthority() {
		return name;
	}

	public void setLogin(LoginPage login) {
		this.login = login;
	}

	@ManyToOne
	public LoginPage getLogin() {
		return login;
	}

	public void setPriority(Integer priority) {
		this.priority = priority;
	}

	/**
	 * 标识角色优先级
	 * */
	public Integer getPriority() {
		return priority;
	}

	public boolean isEnabled() {
		return enabled;
	}

	public void setEnabled(boolean enabled) {
		this.enabled = enabled;
	}

}
